<template>
  <el-container>
    <el-aside width="250px">
      <left @changeTitle="changeTitle"></left>
    </el-aside>
    <el-container>
      <el-header>
        <span>{{ title }}</span>
        <div class="userMsg">
          <div>
            <el-avatar :src="user.avatar?user.avatar:circleUrl" :key="user.avatar"></el-avatar>
            <span>{{ user.userName }}</span>
          </div>
          <span class="loginOut">|</span>
          <span class="loginOut" @click="loginOut">退出</span>
        </div>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import left from "./left.vue";
export default {
  data() {
    return {
      searchVal: "",
      circleUrl:
        "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
      title: "修改个人信息",
      user: "",
    };
  },
  components: {
    left,
  },
  watch:{
    "$store.state.user":{
      handler(val){
        this.user = val;
      },
      deep:true,
    }
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    loginOut() {
      // 清空本地token
      localStorage.setItem("token", ""),
        // 跳到登录页
        this.$router.push({
          name: "login",
        });
    },
    changeTitle(val) {
      this.title = val;
    },
    getUserInfo() {
      let ts = this;
      ts.$http({
        method: "GET",
        url: ts.portInterface.getUserInfo,
        params: {
          userId:  localStorage.getItem('userId'),
        },
      })
        .then((response) => {
          if (response.data.code == 200) {
            this.$store.commit("setUserInfo", response.data.result);
            this.user = response.data.result;
          } else {
            ts.$message({
              showClose: true,
              message: response.data.message,
              type: "error",
            });
          }
        })
        .catch((error) => {
          ts.$message({
            showClose: true,
            message: error,
            type: "error",
          });
        });
    },
  },
};
</script>
<style lang="less" scoped>
header {
  background: none;
  justify-content: space-between;
}
.el-header {
  color: rgba(0, 0, 0, 1);
  .el-row {
    width: 100%;
    height: 100%;
    .el-col {
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
.userMsg {
  height: 100%;
  right: 20px;
  top: 0;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  div {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .loginOut {
    padding: 5px;
    padding-bottom: 20px;
  }
  span {
    // font-size: 0.25rem;
  }
}

@media screen and (max-width: 1200px) {
  .userMsg {
    span {
      //   font-size: 0.6rem;
    }
  }
}
</style>